﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;

namespace RaisingForce.Raise.Language.Model.Rules
{
    sealed public class Rule
    {
        #region Constructors

        public Rule(
            IfWord ifWord,
            ThenWord thenWord,
            ChainWord chainWord)
        {
            if (ifWord == null)
            {
                throw new ArgumentNullException("ifWord");
            }
            if (thenWord == null)
            {
                throw new ArgumentNullException("thenWord");
            }
            if (chainWord == null)
            {
                throw new ArgumentNullException("chainWord");
            }
            Contract.EndContractBlock();
            this.ifWord = ifWord;
            this.thenWord = thenWord;
            this.chainWord = chainWord;
        }

        #endregion

        #region Private Declarations

        private IfWord ifWord;

        private ThenWord thenWord;

        private ChainWord chainWord;

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.ifWord != null);
            Contract.Invariant(this.thenWord != null);
            Contract.Invariant(this.chainWord != null);
        }

        #endregion

        #region Properties

        public IfWord IfWord
        {
            get
            {
                Contract.Ensures(Contract.Result<IfWord>() != null);
                return this.ifWord;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("IfWord");
                }
                Contract.EndContractBlock();
                this.ifWord = value;
            }
        }

        public ThenWord ThenWord
        {
            get
            {
                Contract.Ensures(Contract.Result<ThenWord>() != null);
                return this.thenWord;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("ThenWord");
                }
                Contract.EndContractBlock();
                this.thenWord = value;
            }
        }

        public ChainWord ChainWord
        {
            get
            {
                Contract.Ensures(Contract.Result<ChainWord>() != null);
                return this.chainWord;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("ChainWord");
                }
                Contract.EndContractBlock();
                this.chainWord = value;
            }
        }

        #endregion

        #region Public Methods

        public RuleData GetData()
        {
            Contract.Ensures(Contract.Result<RuleData>() != null);
            return new RuleData(
                this.ifWord.GetData(),
                this.thenWord.GetData(),
                this.chainWord.GetData());
        }

        #endregion

        #region Static Methods

        public static Rule CreateFromData(
            RuleData ruleData,
            IWordFabric wordFabric)
        {
            if (ruleData == null)
            {
                throw new ArgumentNullException("ruleData");
            }
            if (wordFabric == null)
            {
                throw new ArgumentNullException("wordFabric");
            }
            Contract.Ensures(Contract.Result<Rule>() != null);
            Contract.EndContractBlock();
            IfWord ifWord = wordFabric.CreateWord<IfWord>(ruleData.IfWordData);
            ThenWord thenWord = wordFabric.CreateWord<ThenWord>(ruleData.ThenWordData);
            ChainWord chainWord = wordFabric.CreateWord<ChainWord>(ruleData.ChainWordData);
            return new Rule(ifWord, thenWord, chainWord);
        }

        #endregion
    }
}
